package com.have.blog.admin.controller;


import com.have.blog.admin.annotation.operationLogger.OperationLogger;
import com.have.blog.admin.domain.dto.SystemParamDto;
import com.have.blog.admin.service.SystemParamService;
import com.have.blog.base.exception.ThrowableUtils;
import com.have.blog.base.validator.group.GetList;
import com.have.blog.base.validator.group.Insert;
import com.have.blog.base.validator.group.Update;
import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.core.annotion.avoidRepeatableCommit.AvoidRepeatableCommit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 参数配置 RestApi
 */
@RestController
@RequestMapping("/sysParams")
@Api(value = "参数配置相关接口", tags = {"参数配置相关接口"})
@Slf4j
public class SystemParamController {
    @Autowired
    private SystemParamService systemParamService;

    @ApiOperation(value = "获取参数配置列表", notes = "获取参数配置列表", response = String.class)
    @PostMapping("/getList")
    public Wrapper<CommonPage<SystemParamDto>> getList(@Validated({GetList.class}) @RequestBody SystemParamDto systemParamDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取参数配置列表");
        return systemParamService.getPageList(systemParamDto);
    }

    @AvoidRepeatableCommit
    @OperationLogger(value = "增加参数配置")
    @ApiOperation(value = "增加参数配置", notes = "增加参数配置", response = String.class)
    @PostMapping("/add")
    public Wrapper add(@Validated({Insert.class}) @RequestBody SystemParamDto systemParamDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return systemParamService.addSysParams(systemParamDto);
    }

    @OperationLogger(value = "编辑参数配置")
    @ApiOperation(value = "编辑参数配置", notes = "编辑参数配置", response = String.class)
    @PostMapping("/edit")
    public Wrapper edit(HttpServletRequest request, @Validated({Update.class}) @RequestBody SystemParamDto systemParamDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return systemParamService.editSysParams(systemParamDto);
    }

    @OperationLogger(value = "批量删除参数配置")
    @ApiOperation(value = "批量删除参数配置", notes = "批量删除参数配置", response = String.class)
    @PostMapping("/deleteBatch")
    public Wrapper delete(@RequestBody List<Integer> uids) {

        return systemParamService.deleteBatchSysParams(uids);
    }
}
